package com.supostacks.xuetanginterfaces.facade;

import com.alibaba.fastjson.JSONObject;
import com.supostacks.application.application.ICardKeyApplication;
import com.supostacks.application.application.IUserApplication;
import com.supostacks.domain.vo.BaseVo;
import com.supostacks.domain.vo.CardKeyVo;
import com.supostacks.domain.vo.UserVo;
import com.supostacks.rpc.server.ICardKeyExclusiveService;
import com.supostacks.rpc.server.res.XResult;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Calendar;

@DubboService
public class CardKeyExclusiveServiceImpl implements ICardKeyExclusiveService {

    @Resource
    ICardKeyApplication cardKeyApplication;

    @Resource
    IUserApplication userApplication;

    @Override
    public XResult generateCard(int count, String type, String secretKey) {
        try {
            cardKeyApplication.generateCard(count, type, secretKey);
            return XResult.builder().result("生成成功").code(200).msg("success").build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public XResult activateCode(String code, String secretKey, String openid) {
        try {

            CardKeyVo cardKey = cardKeyApplication.selectCardKeyByCode(code);
            if (cardKey == null) {
                return XResult.builder().result("卡密无效").code(500).msg("error").build();
            }
            if (cardKey.getStatus().equals("已使用")) {
                return XResult.builder().result("卡密已使用").code(500).msg("error").build();
            }

            String plaintext = cardKeyApplication.decryptCode(code, secretKey);

            String[] plaintexts = plaintext.split("#=#");
            String type = plaintexts[0];
            String id = plaintexts[1];

            BaseVo baseVo = userApplication.selectById(openid);
            UserVo user;
            if (baseVo instanceof UserVo) {
                user = (UserVo) baseVo;
                user.setAccountType(type);

                Calendar activateCal = Calendar.getInstance();
                Calendar expireCal = Calendar.getInstance();
                switch (type) {
                    case "month":
                        expireCal.add(Calendar.MONTH, 1);
                        user.setLevel("月度会员");
                        break;
                    case "season":
                        expireCal.add(Calendar.MONTH, 3);
                        user.setLevel("季度会员");
                        break;
                    case "year":
                        expireCal.add(Calendar.YEAR, 1);
                        user.setLevel("年度会员");
                        break;
                    case "forever":
                        expireCal.add(Calendar.YEAR, 1000);
                        user.setLevel("永久会员");
                        break;
                }

                user.setActivateTime(activateCal.getTime());
                user.setExpireTime(expireCal.getTime());
                user.setCardKey(code);

                JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(user));
                userApplication.updateById(data);

                BaseVo cardBase = cardKeyApplication.selectById(id);
                if (cardBase instanceof CardKeyVo) {
                    CardKeyVo cardKeyVo = (CardKeyVo) cardBase;
                    cardKeyVo.setStatus("已使用");
                    JSONObject data2 = JSONObject.parseObject(JSONObject.toJSONString(cardKeyVo));
                    cardKeyApplication.updateById(data2);
                }

                return XResult.builder().result("激活成功").code(200).msg("success").build();
            }
            return XResult.builder().result("激活失败").code(500).msg("error").build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
